package com.kz.himalaya.fragment.homepage;


import android.databinding.DataBindingUtil;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v4.view.GravityCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.kz.himalaya.BR;
import com.kz.himalaya.R;
import com.kz.himalaya.adapter.CommentAdapter;
import com.kz.himalaya.application.MyApplication;
import com.kz.himalaya.bean.CityColumnEntity;
import com.kz.himalaya.bean.DiscoveryEntity;
import com.kz.himalaya.bean.EditorRecommend;
import com.kz.himalaya.bean.FocusImagesEntity;
import com.kz.himalaya.bean.GuessEntity;
import com.kz.himalaya.bean.HomeEntity;
import com.kz.himalaya.bean.HomeHotEntity;
import com.kz.himalaya.bean.HotRecommendsEntity;
import com.kz.himalaya.bean.PaidAreaEntity;
import com.kz.himalaya.bean.SpecialColumn;
import com.kz.himalaya.databinding.FragmentHotBinding;
import com.kz.himalaya.interfaces.HttpService;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class HotFragment extends Fragment implements Callback<HomeEntity> {


    private CommentAdapter<Object> adapter;

    public HotFragment() {
        // Required empty public constructor
    }

    public static HotFragment newInstance() {

        Bundle args = new Bundle();

        HotFragment fragment = new HotFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        FragmentHotBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_hot, container, false);
        MyApplication application = (MyApplication) getActivity().getApplication();
        HttpService service = application.getService();
        HashMap<Class<?>, Pair<Integer,Integer>> map = new HashMap<>();
        map.put(null,Pair.create(0,1));
        map.put(FocusImagesEntity.class,Pair.create(R.layout.hot_focus_header, BR.hot_header));
        map.put(GuessEntity.class,Pair.create(R.layout.guess_item,BR.guessData));
        map.put(DiscoveryEntity.class,Pair.create(R.layout.hot_discovery,BR.discovery));
        map.put(PaidAreaEntity.class,Pair.create(R.layout.hot_boutique_column,BR.boutique));
        map.put(EditorRecommend.class,Pair.create(R.layout.hot_editor_item,BR.hotEditorRecommend));
        map.put(CityColumnEntity.class,Pair.create(R.layout.hot_city,BR.hotCityColumn));
        map.put(HotRecommendsEntity.HotRecommendsList.class,Pair.create(R.layout.hot_recommends_list,BR.hotRecommendList));
        map.put(SpecialColumn.class,Pair.create(R.layout.hot_boutique_listen,BR.boutiqueListen));
        adapter = new CommentAdapter<>(map,new ArrayList<>(),getContext());
        binding.listHot.setAdapter(adapter);
        service.getHomeData().enqueue(this);
        service.getHomeHotData().enqueue(new Callback<HomeHotEntity>() {
            @Override
            public void onResponse(Call<HomeHotEntity> call, Response<HomeHotEntity> response) {
                HomeHotEntity body = response.body();
                if (body != null) {
                    if (body.getMsg().equals(getString(R.string.request_success_flag))) {
                        adapter.add(1,body.getDiscoveryColumns());
                        adapter.add(2,body.getGuess());
                        adapter.add(3,body.getPaidArea());
                        adapter.add(5,body.getCityColumn());
                        adapter.addAll(7,body.getHotRecommends().getList());
                    }
                }
            }

            @Override
            public void onFailure(Call<HomeHotEntity> call, Throwable t) {
                t.printStackTrace();
                Log.i("1620", "onFailure: "+t.getMessage());
                Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int position = parent.getChildAdapterPosition(view);
                if (position!=0) {
                    outRect.set(0,0,0,30);
                }
            }
        };
        binding.listHot.addItemDecoration(decoration);
        return binding.getRoot();
    }

    @Override
    public void onResponse(Call<HomeEntity> call, Response<HomeEntity> response) {
        HomeEntity body = response.body();
        if (body != null) {
            if (body.getMsg().equals(getString(R.string.request_success_flag))) {
                adapter.add(0,body.getFocusImages());
                adapter.add(4,body.getEditorRecommendAlbums());
                adapter.add(6,body.getSpecialColumn());
            }
        }
    }

    @Override
    public void onFailure(Call<HomeEntity> call, Throwable t) {
        t.printStackTrace();
        Log.i("1620", "onFailure: "+t.getMessage());
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
